﻿using System.Collections.Generic;
using System.Linq;
using SIG.Repository;
using SIG.Infrastructure.Logging;
using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Configs;
using SIG.Model.Articles;
using System.Linq.Expressions;
using System;
using SIG.Basic.Extensions;
using SIG.Resources.Admin;

namespace SIG.Service
{
    public class ArticleCategoryService : EntityService<ArticleCategory>, IArticleCategoryService
    {

        private readonly IArticleCategoryRepository _categoryRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public ArticleCategoryService(IArticleCategoryRepository categoryRepository, ILoggingService loggingService, ICacheService cacheService)
            : base(categoryRepository, cacheService, loggingService)
        {
            _categoryRepository = categoryRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }


        public ArticleCategory GetCategoryBySeoName(string seoName)
        {

            var key = $"{EntityNames.ArticleCategory}_bySeoName_{seoName}";
            if (!SettingsManager.Article.EnableCaching)
            {
                return _categoryRepository.GetFirstOrDefault(p => p.Active & p.SeoName.Equals(seoName, System.StringComparison.InvariantCultureIgnoreCase));
            }

            if (_cacheService.IsSet(key))
            {
                return (ArticleCategory)_cacheService.Get(key);

            }

            var category = _categoryRepository.GetFirstOrDefault(p => p.Active & p.SeoName.Equals(seoName, System.StringComparison.InvariantCultureIgnoreCase)); ;
            _cacheService.Set(key, category, SettingsManager.Article.CacheDuration);


            return category;
        }

        public List<ArticleCategory> GetPagedElements(int pageIndex, int pageSize, string keyword, out int totalCount)
        {

            //get list count
            var keyCount = $"{EntityNames.ArticleCategory}_ListCount_{keyword}";

            var totalIQuery = _categoryRepository.GetAllElements().Where(g => g.Active);
            if (!string.IsNullOrEmpty(keyword))
                totalIQuery = totalIQuery.Where(g => g.Title.Contains(keyword));

            if (SettingsManager.Article.EnableCaching)
            {
                if (_cacheService.IsSet(keyCount))
                {
                    totalCount = (int)_cacheService.Get(keyCount);
                }
                else
                {
                    totalCount = totalIQuery.Count();
                    _cacheService.Set(keyCount, totalCount, SettingsManager.Article.CacheDuration);
                }
            }
            else
            {
                totalCount = totalIQuery.Count();
            }


            //get list
            var key = $"{EntityNames.ArticleCategory}_List_{pageIndex}_{pageSize}_{keyword}";

            List<ArticleCategory> categories;
            Expression<Func<ArticleCategory, bool>> filter = g => g.Active;
            Expression<Func<ArticleCategory, bool>> filterByKeyword = g => g.Title.Contains(keyword);


            if (!string.IsNullOrEmpty(keyword))
                filter = filter.AndAlso(filterByKeyword);


            if (SettingsManager.Article.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    categories = (List<ArticleCategory>)_cacheService.Get(key);

                }
                else
                {
                    categories = _categoryRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), filter, false).ToList();
                    _cacheService.Set(key, categories, SettingsManager.Article.CacheDuration);
                }
            }
            else
            {
                categories = _categoryRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), filter, false).ToList();
            }

            return categories;

        }

    }
}
